package com.tangdada.whacmole;

import android.graphics.Canvas;
import android.util.Log;

import com.tangdada.whacmole.base.Sprite;

import java.util.Random;


/**
 * 老鼠类
 * Created by wjy on 2018/4/12
 */

public class Mouse extends Sprite {

    public final static int STATE_NONE = 0;
    public final static int STATE_UP = 1;
    public final static int STATE_DOWN = 2;
    public final static int STATE_WAIT = 3;
    public final static int STATE_HIT = 4;
    public final static int STATE_YUN = 5;
    public int state = STATE_NONE;
    public int wait_time = 0;//伸出洞口等待时间
    public int sleep_time = 0;//底下睡眠时间

    public float up_speed = 0;//上升速度
    public float down_speed = 0;//下降速度
    public float initPosY;//初始Y
    public Random random;
    public String tip;
    public boolean isGood;

    public Mouse(String... path) {
        super(path);
    }


    @Override
    public void onUpdate() {
        super.onUpdate();
//        Log.i("wjy", "State:" + state + ",posY:" + posY + ",waitTime:" + wait_time + "sleep_time:" + sleep_time);
        switch (state) {
            case STATE_UP:
                posY -= up_speed;
                if (posY <= initPosY - height * 6 / 5) {
                    posY = initPosY - height * 6 / 5;
                    state = STATE_WAIT;
                }
                break;
            case STATE_WAIT:
                if (wait_time > 0) wait_time--;
                else state = STATE_DOWN;
                break;
            case STATE_HIT:
                if (wait_time > 0) wait_time--;
                else state = STATE_YUN;
                break;
            case STATE_DOWN:
            case STATE_YUN:
                posY += down_speed;
                if (posY >= initPosY) {
                    posY = initPosY;
                    state = STATE_NONE;
                }
                break;
            case STATE_NONE:
                if (sleep_time > 0) {
                    sleep_time--;
                    if (sleep_time == 0) state = STATE_UP;
//                } else {
//                    create(true);
                }
        }
    }

    public boolean isNone() {
        return state == STATE_NONE && sleep_time == 0;
    }

    @Override
    public void onDraw(Canvas c) {
        if (state == STATE_HIT) {
            c.drawBitmap(bitmaps[1], posX, posY, paint);
        } else if (state == STATE_YUN) {
            c.drawBitmap(bitmaps[2], posX, posY, paint);
        } else if (state != STATE_NONE) {
            c.drawBitmap(bitmaps[currentFrame], posX, posY, paint);
        }
    }

    public void reset (){
        state = STATE_NONE;
        posY = initPosY;
    }

    public void create(boolean need_sleep,String tip, boolean good) {
        if (random == null) random = new Random();
        if (need_sleep) {
            sleep_time = random.nextInt(20) + 10;
        } else {
            state = STATE_UP;
        }
        if(initPosY == 0){
            initPosY = posY;
        }
        up_speed = (random.nextInt(10) + 20) * GameActivity.SCALE_Y;
        down_speed = (random.nextInt(10) + 10) * GameActivity.SCALE_Y;
        wait_time = 20;
        isGood = good;
        this.tip = tip;
    }
}
